package com.prs.crm.action.oa;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.cdxh.Cdstock;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Comment;
import com.prs.crm.domain.oa.CommentChild;
import com.prs.crm.domain.oa.NewsReceive;
import com.prs.crm.domain.oa.Notice;
import com.prs.crm.domain.oa.NoticeAccept;
import com.prs.crm.domain.oa.NoticeCollect;
import com.prs.crm.domain.oa.NoticeCollectType;
//import com.prs.crm.domain.oa.NoticeCollect;
import com.prs.crm.service.AttachmentService;
import com.prs.crm.service.cdxh.CdstockService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.oa.CommentChildService;
import com.prs.crm.service.oa.CommentService;
import com.prs.crm.service.oa.NoticeAcceptService;
import com.prs.crm.service.oa.NoticeCollectService;
import com.prs.crm.service.oa.NoticeCollectTypeService;
//import com.prs.crm.service.oa.NoticeCollectService;
import com.prs.crm.service.oa.NoticeService;
import com.sun.jmx.snmp.Timestamp;

public class NoticeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Notice notice;
	private NoticeService noticeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private Integer[] noticeIds;
	private NoticeAccept noticeAccept;
	private NoticeAcceptService noticeAcceptService;
	private Cdstock  cdstock;
	private CdstockService  cdstockService;
	private NoticeCollect noticeCollect;
	private NoticeCollectService noticeCollectService;
	private PersonService personService;
	private NoticeCollectType noticeCollectType;
	private NoticeCollectTypeService noticeCollectTypeService;
	private String accepts;
	private File upload;
	private String uploadFileName;
	private Integer[] receiveIds;
	private String type;
	private Integer[] collectIds;
	private AttachmentService attachmentService;
	private CommentService commentService;
	private CommentChildService commentChildService;
	public CommentService getCommentService() {
		return commentService;
	}
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}
	public CommentChildService getCommentChildService() {
		return commentChildService;
	}
	public void setCommentChildService(CommentChildService commentChildService) {
		this.commentChildService = commentChildService;
	}
	public AttachmentService getAttachmentService() {
		return attachmentService;
	}
	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}
	@Action("listNotice")
	public String execute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Notice.class);
		criteria.add(Restrictions.eq("oabox", 1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoticeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listNoticeOA")
	public String listNoticeOA() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Notice.class);
		criteria.add(Restrictions.eq("oabox", 1));
		criteria.add(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoticeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listNoticeDraft")   
	public String listNoticeDraft() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Notice.class);
		criteria.add(Restrictions.eq("oabox", 0));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoticeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listNoticeReceiveOA")
	public String listNoticeReceiveOA() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NoticeAccept.class);
		criteria.add(Restrictions.eq("oabox", 1));
		criteria.add(Restrictions.eq("receivePerson.id", AppContext.getAccountContext().currentUser().getId()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoticeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listNoticeCollectOA")
	public String listNoticeCollectOA() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NoticeCollect.class);
		criteria.add(Restrictions.eq("recorder.id", AppContext.getAccountContext().currentUser().getId()));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoticeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listNoticeCollectType")
	public String listNoticeCollectType(){
		DetachedCriteria crit = this.getPaginationHelper().createQuery(NoticeCollectType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoticeService().listByPage(crit,
				pagination));
		return SUCCESS;
	}
	@Action("forWardNoticeReceive")
	public String forWardNoticeReceive() {
		if(this.receiveIds!=null){
			this.noticeAccept=this.noticeAcceptService.get(receiveIds[0]);
//			this.notice=noticeAccept.getNotice();
		}
		notice=new Notice();
		notice.setContent("<BR><BR>----- Original Message ----- <BR>" +
						"From: "+noticeAccept.getRecorder().getResume().getName()+"<BR>" +
						"To："+noticeAccept.getReceivePerson().getResume().getName()+"<BR>" +
						"Title："+noticeAccept.getTitle()+"<BR>" +
						"Content: <BR>"+noticeAccept.getContent());
		notice.setReceive("");
		notice.setTitle("Fw: "+noticeAccept.getTitle());
		return SUCCESS;
	}
	@Action("viewNotice")
	public String view() {
//		accepts = "";
//		Iterator<NoticeAccept> iter = notice.getAccepts().iterator();
//		while (iter.hasNext()) {
//			accepts += iter.next().getPerson().getResume().getName() + " , ";
//		}
		return SUCCESS;
	}
	@Action("editNoticeOA")
	public String editNoticeOA() {
		if(noticeIds!=null){
			this.notice=this.getNoticeService().get(noticeIds[0]);
		}
		return SUCCESS;
	}
	@Action("editNoticeDraft")
	public String editNoticeDraft() {
		if(noticeIds!=null){
			this.notice=this.noticeService.get(noticeIds[0]);
		}
		return SUCCESS;
	}
	@Action("viewNoticeOA")
	public String viewNoticeOA() {
		if(noticeIds!=null){
			this.notice=this.noticeService.get(noticeIds[0]);
		}
		return SUCCESS;
	}
	@Action("viewNoticeReceive")
	public String viewNoticeReceive() {
		if(noticeAccept!=null){
		if(noticeAccept.getState().equals("未读")){
			noticeAccept.setState("已读");
			this.noticeAcceptService.submit(noticeAccept);
		}}
		return SUCCESS;
	}
	@Action(value = "getNoticeToDraft", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeDraft"),
			@Result(name = "error", type = "chain", location = "editNoticeDraft?notice=${notice.getId()}") })
	public String getNoticeToDraft() {
		if(upload!=null){
			Attachment attach=FileUtil.getInstance().createAttach(upload, uploadFileName);
			this.getAttachmentService().save(attach);
			notice.setAttach(attach);
		}
		this.notice.setOabox(0);
		this.noticeService.submit(notice);
		return SUCCESS;
	}
	@Action(value = "removeNoticeReceive", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeReceiveOA"),
			@Result(name = "error", type = "chain", location = "listNoticeReceiveOA") })
	public String removeNoticeReceive() {
		for (Integer id : receiveIds) {
			noticeAccept=this.getNoticeAcceptService().get(id);
			for (int i = 0; i < noticeAccept.getComment().size(); i++) {
			Comment c=noticeAccept.getComment().get(i);
				for (int j = 0; j < c.getChilds().size(); j++) {
				CommentChild cc=c.getChilds().get(j);
				this.getCommentChildService().remove(cc);
			    }
				this.getCommentService().remove(c);
			}
		}
		this.noticeAcceptService.batchRemove(receiveIds);
		return SUCCESS;
	}
//	@Action(value = "removeNoticeCollect", results = {
//			@Result(name = "success", type = "redirect", location = "listNoticeCollectOA"),
//			@Result(name = "error", type = "chain", location = "listNoticeCollectOA") })
//	public String removeNoticeCollect() {
//		this.noticeCollectService.batchRemove(this.collectIds);
//		return SUCCESS;
//	}
	@Action(value = "deleteNoticeReceive", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeReceiveOA"),
			@Result(name = "error", type = "chain", location = "viewNoticeReceive?noticeAccept=%{noticeAccept.getId()}") })
	public String deleteNoticeReceive() {
		for (int i = 0; i < noticeAccept.getComment().size(); i++) {
			Comment c=noticeAccept.getComment().get(i);
				for (int j = 0; j < c.getChilds().size(); j++) {
				CommentChild cc=c.getChilds().get(j);
				this.getCommentChildService().remove(cc);
			    }
				this.getCommentService().remove(c);
			}
		this.noticeAcceptService.remove(noticeAccept);
		return SUCCESS;
	}
	@Action("ReplyNotice")
	public String ReplyNotice() {
		if(receiveIds!=null){
			this.noticeAccept=this.noticeAcceptService.get(receiveIds[0]);
//			this.notice=noticeAccept.getNotice();
		}
		Notice nc=this.getNoticeService().getNoticeId(noticeAccept.getNoticeId());
		if (nc!=null) {
			if(type.equals("1")){  //回复全部
				notice=new Notice();
				notice.setReceive(nc.getReceive());
				notice.setContent("<BR><BR>----- Original Message ----- <BR>" +
								"From: "+nc.getRecorder().getResume().getName()+"<BR>" +
								"To："+nc.getReceive()+"<BR>" +
								"Title："+nc.getTitle()+"<BR>" +
								"Content: <BR>"+nc.getContent());
				this.accepts=nc.getAccept();
				notice.setTitle("Re: "+nc.getTitle());
			}else{
				notice=new Notice();
				notice.setReceive(nc.getReceive());
				String[] receive=nc.getReceive().split(",");
				String[] acc=nc.getAccept().split(",");
				notice.setContent("<BR><BR>----- Original Message ----- <BR>" +
						"From: "+nc.getRecorder().getResume().getName()+"<BR>" +
						"To："+receive[0]+"<BR>" +
						"Title："+nc.getTitle()+"<BR>" +
						"Content: <BR>"+nc.getContent());
				this.accepts=acc[0];
				notice.setTitle("Re: "+nc.getTitle());
			}
		}
		return SUCCESS;
	}
	@Action(value = "saveNoticeDraft", results = {
			@Result(name = "success", type = "redirect", location = "editNoticeOA"),
			@Result(name = "error", type = "chain", location = "editNoticeOA") })
	public String saveNoticeDraft() {
		if(upload!=null){
			Attachment attach=FileUtil.getInstance().createAttach(upload, uploadFileName);
			this.getAttachmentService().save(attach);
			notice.setAttach(attach);
		}
		notice.setOabox(0);
		notice.setAccept(accepts);
		if(notice.getId()==null){
			this.noticeService.save(notice);
		}else{
			this.noticeService.submit(notice);
		}
		this.setPrompt("保存通知到草稿箱成功");
		return SUCCESS;
	}
	@Action(value = "saveNoticeReceiveDraft", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeReceiveOA"),
			@Result(name = "error", type = "chain", location = "ReplyNotice?notice=${notice.getId()}") })
	public String saveNoticeReceiveDraft() {
		this.notice.setOabox(0);
		notice.setAccept(accepts);
		this.noticeService.submit(notice);
		this.noticeAcceptService.remove(this.noticeAccept);
		this.setPrompt("保存通知到草稿箱成功");
		return SUCCESS;
	}
	@Action(value = "saveNoticeToCollect", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeOA"),
			@Result(name = "error", type = "chain", location = "viewNoticeOA?notice=${notice.getId()}") })
	public String saveNoticeToCollect() {
		notice.setOabox(3);
		this.getNoticeService().submit(notice);
		noticeCollect=new NoticeCollect();
		noticeCollectType=this.noticeCollectTypeService.getTypeByName("已发通知");
		if(this.noticeCollectType==null){
			noticeCollectType=new NoticeCollectType();
			noticeCollectType.setName("已发通知");
			this.noticeCollectTypeService.save(noticeCollectType);
		}
		this.noticeCollect.setType(noticeCollectType);
		this.noticeCollect.setNotice(notice);
		this.noticeCollectService.save(noticeCollect);
		this.setPrompt("收藏通知成功");
		return SUCCESS;
	}
	@Action("forWardNotice")
	public String forWardPictureNews() {
		if(this.noticeIds!=null){
			this.notice=this.noticeService.get(noticeIds[0]);
		}
		notice.setContent("<BR><BR>----- Original Message ----- <BR>" +
						"From: "+notice.getRecorder().getResume().getName()+"<BR>" +
						"To："+notice.getReceive()+"<BR>" +
						"Title："+notice.getTitle()+"<BR>" +
						"Content: <BR>"+notice.getContent());
		notice.setReceive("");
		notice.setTitle("Fw: "+notice.getTitle());
		if (notice.getAttach()==null) {
			notice.setAttach(notice.getAttach());
		}
		return SUCCESS;
	}
	@Action(value = "removeNoticeOA", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeOA"),
			@Result(name = "error", type = "chain", location = "listNoticeOA") })
	public String removeNoticeOA() {
		for(int i=0;i<noticeIds.length;i++){
			this.notice=this.noticeService.get(noticeIds[i]);
//			if(notice.getAccepts().size()!=0){
//				this.addActionError("通知已发送，不能删除");
//				return ERROR;
//			}else 
			if(notice.getCollects().size()!=0){
				this.addActionError("通知已收藏，不能删除");
				return ERROR;
			}
			for (int a = 0; a < notice.getComment().size(); a++) {
				Comment c=notice.getComment().get(a);
					for (int j = 0; j < c.getChilds().size(); j++) {
					CommentChild cc=c.getChilds().get(j);
					this.getCommentChildService().remove(cc);
				    }
					this.getCommentService().remove(c);
				}
		}
		this.noticeService.batchRemove(noticeIds);
		return SUCCESS;
	}
	@Action(value = "deleteNotice", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeOA"),
			@Result(name = "error", type = "chain", location = "viewNoticeOA?notice=${notice.getId()}") })
	public String deleteNotice() {
//		if(notice.getAccepts().size()!=0){
//			this.addActionError("通知已发送，不能删除");
//			return ERROR;
//		}else 
		if(notice.getCollects().size()!=0){
			this.addActionError("通知已收藏，不能删除");
			return ERROR;
		}
		for (int a = 0; a < notice.getComment().size(); a++) {
			Comment c=notice.getComment().get(a);
				for (int j = 0; j < c.getChilds().size(); j++) {
				CommentChild cc=c.getChilds().get(j);
				this.getCommentChildService().remove(cc);
			    }
				this.getCommentService().remove(c);
			}
		this.noticeService.remove(this.notice);
		return SUCCESS;
	}
	@Action("listUnReadNotice")
	public String listUnReadNotice() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NoticeAccept.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("person", person));
		criteria.add(Restrictions.eq("state", Constrains.NOTICE_UNREAD));
		this.setPagination(this.getNoticeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("listSendNotice")
	public String listSendNotice() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Notice.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("recorder", person));
		this.setPagination(this.getNoticeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}


	@Action(value = "ajaxReadNotice", results = { @Result(name = "success", type = "json", params = {
			"root", "prompt" }) })
	public String ajaxReadNotice() {
		Collection<Notice> notis = this.getNoticeService().getUnreadNotice(
				AppContext.getAccountContext().currentUser());
		if (notis.size() > 0)
			this.setPrompt("true");
		else
			this.setPrompt("false");
		return SUCCESS;
	}
	//已收通知
	@Action("listReadedNotice")
	public String listReadedNotice() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				NoticeAccept.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("person", person));
		criteria.add(Restrictions.eq("oabox", 1));
		this.setPagination(this.getNoticeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	//草稿箱列表
	@Action("listCaogaoNotice")
	public String listCaogaoNotice(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Notice.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("recorder", person));
		criteria.add(Restrictions.eq("oabox", 0));
		this.setPagination(this.getNoticeService().listByPage(criteria, pagination));
		return SUCCESS;
	}

	@Action("readNotice")
	public String readNotice() {
		if(noticeAccept.getTitle()=="安全库存"){
			this.getNoticeAcceptService().save(noticeAccept);	
		}
		else{
		noticeAccept.setState(Constrains.NOTICE_READED);
		this.getNoticeAcceptService().save(noticeAccept);
		}
		return SUCCESS;
	}
	@Action("viewNoticeCdstock")
	public String viewNoticeCdstock() {
		
		return SUCCESS;
	} 
	
	@Action("viewCaogaoNotice")
	public String viewCaogaoNotice(){
		return SUCCESS;
	}
	
	@Action("editNotice")
	public String edit() {
		return SUCCESS;
	}
	
	@Action("editCaogaoNotice")
	public String editCaogaoNotice(){
		return SUCCESS;
	}
	@Action(value = "sendNotice", results = {
			@Result(name = "success", type = "redirect", location = "editNoticeOA"),
			@Result(name = "error", type = "chain", location = "editNoticeOA") })
	public String sendNews() {
		//已发新闻设置
		notice.setOabox(1);//0表示草稿
		notice.setTop(0);//表示没有置顶
		notice.setCreatTime(new Date());
		if(upload!=null){
			Attachment attach=FileUtil.getInstance().createAttach(upload, uploadFileName);
			this.getAttachmentService().save(attach);
			notice.setAttach(attach);
		}
		if (notice.getId()==null) {
			notice.setAccept(accepts);
		}
		this.noticeService.submit(notice);
		//已收新闻设置
		Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(notice.getAccept().split(",")));
		Iterator<Person> iter = acceptPersons.iterator();
		while(iter.hasNext()) {
			Person person = iter.next();
			NoticeAccept re=new NoticeAccept();
			re.setNoticeId(notice.getId());
			re.setTitle(notice.getTitle());
			re.setContent(notice.getContent());
			re.setDegree(notice.getDegree());
			re.setIsComment(notice.getIsComment());
			if(notice.getAttach()!=null){
				re.setAttach(notice.getAttach());
			}
			re.setOabox(1);
			re.setSendPerson(AppContext.getAccountContext().currentUser()); //发送人
			re.setReceivePerson(person);  //接收人
			re.setOabox(1);
			re.setState("未读");
			this.noticeAcceptService.save(re);
		}
		this.setPrompt("新闻发送成功");
		return SUCCESS;
	}
	@Action(value = "sendNoticeReceive", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeReceiveOA"),
			@Result(name = "error", type = "chain", location = "ReplyNotice?notice=${notice.getId()}") })
	public String sendNoticeReceive() {
		//已发新闻设置
		notice.setOabox(1);//0表示草稿
		notice.setCreatTime(new Date());
		if(upload!=null){
			Attachment attach=FileUtil.getInstance().createAttach(upload, uploadFileName);
			this.getAttachmentService().save(attach);
			notice.setAttach(attach);
		}
		if (notice.getId()==null) {
			notice.setAccept(accepts);
		}
		this.noticeService.save(notice);
		//已收新闻设置
		Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(notice.getAccept().split(",")));
		Iterator<Person> iter = acceptPersons.iterator();
		while(iter.hasNext()) {
			Person person = iter.next();
			NoticeAccept re=new NoticeAccept();
			re.setNoticeId(notice.getId());
			re.setTitle(notice.getTitle());
			re.setContent(notice.getContent());
			re.setDegree(notice.getDegree());
			re.setIsComment(notice.getIsComment());
			if(notice.getAttach()!=null){
				re.setAttach(notice.getAttach());
			}
			re.setOabox(1);
			re.setSendPerson(AppContext.getAccountContext().currentUser()); //发送人
			re.setReceivePerson(person);  //接收人
			re.setState("未读");
			re.setOabox(1);
			this.noticeAcceptService.save(re);
		}
		this.setPrompt("新闻发送成功");
		return SUCCESS;
	}
	@Action(value = "sendforWardNotice", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeOA"),
			@Result(name = "error", type = "chain", location = "listNoticeOA") })
	public String sendforWardNotice() {
		//已发新闻设置
		notice.setOabox(1);//0表示草稿
		notice.setCreatTime(new Date());
		if(upload!=null){
			Attachment attach=FileUtil.getInstance().createAttach(upload, uploadFileName);
			this.getAttachmentService().save(attach);
			notice.setAttach(attach);
		}
		if (notice.getId()==null) {
			notice.setAccept(accepts);
		}
		this.noticeService.save(notice);
		//已收新闻设置
		Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(notice.getAccept().split(",")));
		Iterator<Person> iter = acceptPersons.iterator();
		while(iter.hasNext()) {
			Person person = iter.next();
			NoticeAccept re=new NoticeAccept();
			re.setNoticeId(notice.getId());
			re.setTitle(notice.getTitle());
			re.setContent(notice.getContent());
			re.setDegree(notice.getDegree());
			re.setIsComment(notice.getIsComment());
			if(notice.getAttach()!=null){
				re.setAttach(notice.getAttach());
			}
			re.setOabox(1);
			re.setSendPerson(AppContext.getAccountContext().currentUser()); //发送人
			re.setReceivePerson(person);  //接收人
			re.setState("未读");
			re.setOabox(1);
			this.noticeAcceptService.save(re);
		}
		this.setPrompt("新闻发送成功");
		return SUCCESS;
	}
	@Action(value = "sendNoticeDraft", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeDraft"),
			@Result(name = "error", type = "chain", location = "listNoticeDraft") })
	public String sendNoticeDraft() {
		//已发新闻设置
		if(noticeIds!=null){
			this.notice=this.noticeService.get(noticeIds[0]);
		}
		notice.setOabox(1);//0表示草稿
		notice.setCreatTime(new Date());
		if(upload!=null){
			Attachment attach=FileUtil.getInstance().createAttach(upload, uploadFileName);
			this.getAttachmentService().save(attach);
			notice.setAttach(attach);
		}
		if (notice.getId()==null) {
			notice.setAccept(accepts);
		}
		this.noticeService.save(notice);
		//已收新闻设置
		Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(notice.getAccept().split(",")));
		Iterator<Person> iter = acceptPersons.iterator();
		while(iter.hasNext()) {
			Person person = iter.next();
			NoticeAccept re=new NoticeAccept();
			re.setNoticeId(notice.getId());
			re.setTitle(notice.getTitle());
			re.setContent(notice.getContent());
			re.setDegree(notice.getDegree());
			re.setIsComment(notice.getIsComment());
			if(notice.getAttach()!=null){
				re.setAttach(notice.getAttach());
			}
			re.setOabox(1);
			re.setSendPerson(AppContext.getAccountContext().currentUser()); //发送人
			re.setReceivePerson(person);  //接收人
			re.setState("未读");
			re.setOabox(1);
			this.noticeAcceptService.save(re);
		}
		this.setPrompt("通知发送成功");
		return SUCCESS;
	}
	@Action(value = "sendNoticeDraft2", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeDraft"),
			@Result(name = "error", type = "chain", location = "editNoticeDraft?notice=${notice.getId()}") })
	public String sendNoticeDraft2() {
		//已发新闻设置
		notice.setOabox(1);//0表示草稿
		notice.setCreatTime(new Date());
		if(upload!=null){
			Attachment attach=FileUtil.getInstance().createAttach(upload, uploadFileName);
			this.getAttachmentService().save(attach);
			notice.setAccept(accepts);
		}
		this.noticeService.save(notice);
		//已收新闻设置
		Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(notice.getAccept().split(",")));
		Iterator<Person> iter = acceptPersons.iterator();
		while(iter.hasNext()) {
			Person person = iter.next();
			NoticeAccept re=new NoticeAccept();
			re.setNoticeId(notice.getId());
			re.setTitle(notice.getTitle());
			re.setContent(notice.getContent());
			re.setDegree(notice.getDegree());
			re.setIsComment(notice.getIsComment());
			if(notice.getAttach()!=null){
				re.setAttach(notice.getAttach());
			}
			re.setOabox(1);
			re.setSendPerson(AppContext.getAccountContext().currentUser()); //发送人
			re.setReceivePerson(person);  //接收人
			re.setState("未读");
			re.setOabox(1);
			this.noticeAcceptService.save(re);
		}
		this.setPrompt("新闻发送成功");
		return SUCCESS;
	}
	@Clew(results = { "success" })
	@Action(value = "saveNotice", results = {
			@Result(name = "success", type = "redirect", location = "editNotice"),
			@Result(name = "error", type = "chain", location = "editNotice") })
	public String save() {
		if (notice.getTitle() == null || "".equals(notice.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		if (accepts == null || "".equals(accepts.trim())) {
			addActionError("接收人不能为空");
			return ERROR;
		}
		this.setPrompt("创建通知成功");
		notice.setAccept(accepts);
		notice.setOabox(1);
		this.getNoticeService().saveNotice(notice, upload, uploadFileName,
				accepts);
		return SUCCESS;
	}
	
	@Clew(results = { "success" })
	@Action(value = "saveNotice2", results = {
			@Result(name = "success", type = "redirect", location = "listCaogaoNotice"),
			@Result(name = "error", type = "chain", location = "editCaogaoNotice") })
	public String saveNotice2() {
		if (notice.getTitle() == null || "".equals(notice.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		if (accepts == null || "".equals(accepts.trim())) {
			addActionError("收件人不能为空");
			return ERROR;
		}
		if (notice.getContent() == null || notice.getContent().trim().equals("")) {
			addActionError("通知内容不能为空");
			return ERROR;
		}
		this.setPrompt("通知发送成功");
		notice.setOabox(1);
		this.getNoticeService().saveNotice(notice, upload, uploadFileName,
				accepts);
		return SUCCESS;
	}
	//保存为草稿
	@Clew(results = { "success" })
	@Action(value = "saveNoticeToCaogao", results = {
			@Result(name = "success", type = "redirect", location = "editNotice"),
			@Result(name = "error", type = "chain", location = "editNotice") })
	public String saveNoticeToCaogao() {
		if (notice.getTitle() == null || "".equals(notice.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		this.setPrompt("已经保存通知到草稿箱");
		notice.setOabox(0);
		this.getNoticeService().saveNoticeToCaogao(notice, upload, uploadFileName);
		return SUCCESS;
	}
	//修改草稿
	@Action("editNoticeToCaogao")
	public String editNoticeToCaogao(){
		return SUCCESS;
	}
	@Action(value="saveEditNoice",results={
			@Result(name = "success", type = "redirect", location = "listCaogaoNotice"),
			@Result(name = "error", type = "chain", location = "saveEditNotice")
	})
	public String saveEditNotice(){
		if (notice.getTitle() == null || "".equals(notice.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
//		notice.setAccept(accepts);
		Person person = AppContext.getAccountContext().currentUser();
		notice.setRecorder(person);
		this.getNoticeService().saveNotice(notice, upload, uploadFileName,notice.getAccept());
		return SUCCESS;
	}
	@Action(value = "removeNotice", results = {
			@Result(name = "success", type = "redirect", location = "listNotice"),
			@Result(name = "error", type = "chain", location = "listNotice") })
	public String remove() {
		if (ids == null) {
			this.addActionError("请选择要删除的通知");
			return ERROR;
		}
//		for (Integer id : ids) {
//			Notice notice = this.getNoticeService().get(id);
//			if (notice.getAccept().size() != 0) {
//				this.addActionError("通知已发放，不能删除");
//				return ERROR;
//			}
//		}
		this.getNoticeService().batchRemove(ids);
		return SUCCESS;
	}
	
	@Action(value = "insertNoticeToCollect", results = {
			@Result(name = "success", type = "redirect", location = "listNotice"),
			@Result(name = "error", type = "chain", location = "listNotice") })
	public String insertNoticeToCollect() {
		if (ids == null) {
			this.addActionError("请选择要收藏的通知");
			return ERROR;
		}
		for (Integer id : ids) {
			Notice notice = this.getNoticeService().get(id);
			NoticeCollect collect = new NoticeCollect();
			collect.setNotice(notice);
			this.getNoticeCollectService().save(collect);
		}
		return SUCCESS;
	}
	
	@Action(value = "removeCaogaoNotice", results = {
			@Result(name = "success", type = "redirect", location = "listCaogaoNotice"),
			@Result(name = "error", type = "chain", location = "listCaogaoNotice") })
	public String removeCaogaoNotice(){
		if(ids == null){
			this.addActionError("请选择要删除的通知");
			return ERROR;
		}
		this.getNoticeService().batchRemove(ids);
		return SUCCESS;
	}
	@Action(value = "saveNoticeByView", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeReceiveOA"),
			@Result(name = "error", type = "chain", location = "viewNoticeReceive?noticeAccept=${noticeAccept.getId()}") })
	public String saveNoticeByView(){
//		this.notice=this.noticeAccept.getNotice();
		noticeAccept.setOabox(3);
		this.noticeAcceptService.submit(noticeAccept);
		noticeCollect=new NoticeCollect();
		this.noticeCollectType=this.noticeCollectTypeService.getTypeByName("已收通知");
		if(noticeCollectType==null){
			noticeCollectType=new NoticeCollectType();
			noticeCollectType.setName("已收通知");
			this.noticeCollectTypeService.save(noticeCollectType);
		}
		this.noticeCollect.setNoticeAccept(noticeAccept);
		noticeCollect.setType(noticeCollectType);
		this.noticeCollectService.save(noticeCollect);
		
		return SUCCESS;
	}
	/***************已收通知*******************/
	//已收通知的删除
	@Action(value = "removeNoticeAccept", results = {
			@Result(name = "success", type = "redirect", location = "listReadedNotice"), 
			@Result(name = "error", type = "chain", location = "listReadedNotice")})
	public String removeNoticeAccept() {
		if(ids == null){
			this.addActionError("请选择要删除的通知");
			return ERROR;
		}
		this.getNoticeAcceptService().batchRemove(ids);
		return SUCCESS;
	}
	@Action(value = "collectionNotice", results = {
			@Result(name = "success", type = "redirect", location = "listReadedNotice"), 
			@Result(name = "error", type = "chain", location = "listReadedNotice")})
	public String collectionNotice(){
		if(ids == null){
			this.addActionError("请选择要收藏的通知");
			return ERROR;
		}
		for (Integer id : ids) {
			Notice notice = this.getNoticeService().get(id);
			NoticeCollect collect = new NoticeCollect();
			NoticeCollectType type = new NoticeCollectType();
			type.setId(1);
			collect.setNotice(notice);	
			collect.setType(type);
			this.getNoticeCollectService().save(collect);
		}
		return SUCCESS;
	}

//	@Action("listNoticeOAs")
//	public String listNoticeOAs() {
//		DetachedCriteria criteria = this.getPaginationHelper().createQuery(Notice.class);
//		criteria.add(Restrictions.eq("oabox", 1));
//		pagination = this.getPaginationHelper().prepare(pagination);
//		this.setPagination(this.getNoticeService().listByPage(criteria,
//				pagination));
//		return SUCCESS;
//	}
	@Action("listNoticeOAs")
	public String listNoticeOAs(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(Notice.class)
		.add(Restrictions.eq("oabox", 1));
		criteria.addOrder(Order.desc("top"));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getNoticeService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action(value = "saveNoticeTop", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeOAs"),
			@Result(name = "error", type = "chain", location = "listNoticeOAs") })
	public String saveNoticeTop(){
		if(noticeIds!=null){
			this.notice=this.getNoticeService().get(noticeIds[0]);
		}
		notice.setTop(1);
		this.getNoticeService().save(notice);
		return SUCCESS;
	}
	
	@Action(value = "saveNotTop", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeOAs"),
			@Result(name = "error", type = "chain", location = "listNoticeOAs") })
	public String saveNotTop(){
		if(noticeIds!=null){
			this.notice=this.getNoticeService().get(noticeIds[0]);
		}
		notice.setTop(0);
		this.getNoticeService().save(notice);
		return SUCCESS;
	}
	
	@Action(value = "removeNoticeOAs", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeOAs"),
			@Result(name = "error", type = "chain", location = "listNoticeOAs") })
	public String removeNoticeOAs() {
		for(int i=0;i<noticeIds.length;i++){
			this.notice=this.noticeService.get(noticeIds[i]);
//			if(notice.getAccept().size()!=0){
//				this.addActionError("通知已发送，不能删除");
//				return ERROR;
//			}else 
			if(notice.getCollects().size()!=0){
				this.addActionError("通知已收藏，不能删除");
				return ERROR;
			}
		}
		this.noticeService.batchRemove(noticeIds);
		return SUCCESS;
	}
	@Action("viewNoticeOAs")
	public String viewNoticeOAs() {
		if(noticeIds!=null){
			this.notice=this.noticeService.get(noticeIds[0]);
		}
		return SUCCESS;
	}
	
	@Action(value = "removeDraftNotice", results = {
			@Result(name = "success", type = "redirect", location = "listNoticeDraft"),
			@Result(name = "error", type = "chain", location = "listNoticeDraft") })
	public String removeDraftNotice() {
		if (noticeIds==null) {
			this.addActionError("请选择要删除的通知！");
			return ERROR;
		}
		this.getNoticeService().batchRemove(noticeIds);
		return SUCCESS;
	}
	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setAccepts(String accepts) {
		this.accepts = accepts;
	}

	public String getAccepts() {
		return accepts;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public NoticeAccept getNoticeAccept() {
		return noticeAccept;
	}

	public void setNoticeAccept(NoticeAccept noticeAccept) {
		this.noticeAccept = noticeAccept;
	}

	public NoticeAcceptService getNoticeAcceptService() {
		return noticeAcceptService;
	}

	public void setNoticeAcceptService(NoticeAcceptService noticeAcceptService) {
		this.noticeAcceptService = noticeAcceptService;
	}

	public Cdstock getCdstock() {
		return cdstock;
	}

	public void setCdstock(Cdstock cdstock) {
		this.cdstock = cdstock;
	}

	public CdstockService getCdstockService() {
		return cdstockService;
	}

	public void setCdstockService(CdstockService cdstockService) {
		this.cdstockService = cdstockService;
	}


	public NoticeCollect getNoticeCollect() {
		return noticeCollect;
	}


	public void setNoticeCollect(NoticeCollect noticeCollect) {
		this.noticeCollect = noticeCollect;
	}


	public NoticeCollectService getNoticeCollectService() {
		return noticeCollectService;
	}


	public void setNoticeCollectService(NoticeCollectService noticeCollectService) {
		this.noticeCollectService = noticeCollectService;
	}


	public PersonService getPersonService() {
		return personService;
	}


	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public Integer[] getNoticeIds() {
		return noticeIds;
	}
	public void setNoticeIds(Integer[] noticeIds) {
		this.noticeIds = noticeIds;
	}
	public NoticeCollectType getNoticeCollectType() {
		return noticeCollectType;
	}
	public void setNoticeCollectType(NoticeCollectType noticeCollectType) {
		this.noticeCollectType = noticeCollectType;
	}
	public NoticeCollectTypeService getNoticeCollectTypeService() {
		return noticeCollectTypeService;
	}
	public void setNoticeCollectTypeService(
			NoticeCollectTypeService noticeCollectTypeService) {
		this.noticeCollectTypeService = noticeCollectTypeService;
	}
	public Integer[] getReceiveIds() {
		return receiveIds;
	}
	public void setReceiveIds(Integer[] receiveIds) {
		this.receiveIds = receiveIds;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Integer[] getCollectIds() {
		return collectIds;
	}
	public void setCollectIds(Integer[] collectIds) {
		this.collectIds = collectIds;
	}
	
}
